package com.wanyang.task;

/**
 * 定时检查异步扫描的任务，目前只有视频是异步扫描的
 */

import cn.hutool.core.collection.CollectionUtil;
import com.wanyang.aliyun.service.AliyunContentScanService;
import com.wanyang.aliyun.vo.AsyncScanResult;
import com.wanyang.dos.AsyncScanRecordDO;
import com.wanyang.dos.AsyncScanRecordDOExample;
import com.wanyang.dos.PostDO;
import com.wanyang.mapper.AsyncScanRecordDOMapper;
import com.wanyang.mapper.PostDOMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@EnableScheduling
@Slf4j
public class AsyncScanResultQueryTask {

    @Autowired
    private AliyunContentScanService aliyunContentScanService;

    @Resource
    private AsyncScanRecordDOMapper asyncScanRecordDOMapper;

    @Resource
    private PostDOMapper postDOMapper;

    @Scheduled(cron="0 0/5 * * * *")
    @Transactional
    public void queryScanResult() {
        log.info("execute queryScanResult");

        AsyncScanRecordDOExample example = new AsyncScanRecordDOExample();
        example.setOrderByClause("create_time limit 500");

        List<AsyncScanRecordDO> scanRecordDOList = asyncScanRecordDOMapper.selectByExample(example);

        if (CollectionUtil.isEmpty(scanRecordDOList)) {
            log.info("no scanRecord");
            return;
        }

        List<String> taskIds = new ArrayList<>();

        for (AsyncScanRecordDO asyncScanRecordDO : scanRecordDOList) {
            taskIds.add(asyncScanRecordDO.getTaskId());
        }


        List<AsyncScanResult> results = aliyunContentScanService.queryVideoAsyncScanResult(taskIds);

        if (CollectionUtil.isEmpty(results)) {
            return;
        }

        List<String> deleteTaskIds = new ArrayList<>();

        for (int i = 0; i < results.size(); i++) {
            AsyncScanResult result = results.get(i);
            if (result.isDone()) {
                if (result.isPass()) {
                    deleteTaskIds.add(result.getTaskId());
                } else {
                    AsyncScanRecordDO asyncScanRecordDO = scanRecordDOList.get(i);

                    // 目前异步扫描只有帖子的视频
                    Integer businessId = asyncScanRecordDO.getBusinessId();
                    PostDO update = new PostDO();
                    update.setId(businessId);
                    update.setIsApproved(false);
                    postDOMapper.updateByPrimaryKeySelective(update);

                    deleteTaskIds.add(result.getTaskId());
                }

            }
        }

        AsyncScanRecordDOExample deleteExample = new AsyncScanRecordDOExample();
        deleteExample.createCriteria().andTaskIdIn(deleteTaskIds);
        asyncScanRecordDOMapper.deleteByExample(deleteExample);

    }

}
